'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 来袭扫荡
 * @author sundbcn@gmail.com (sun debing)
 */

var async = require('async');
var _ = require('lodash');
var Code = require('../../../consts/code');
var moduleType = require('../../../consts/moduleType');
var operationType = require('../../../consts/operationType');
var PlayerManager = require('../../../libs/playerManager');
var rebelConf = require('../../../config/rebelConf');
var vipConf = require('../../../config/vipConf');
var rebelService = require('../../../services/rebelService');
var rewardService = require('../../../services/rewardService');
var taskService = require('../../../services/taskService');

module.exports = function (app) {
    return new Handler(app);
};

var Handler = function (app) {
    this.app = app;
};

Handler.prototype.sweep = function (msg, session, next) {
    var role_id = session.uid;
    var rebel_id = msg.rebel_id;
    if (!rebel_id) {
        return next(null, {
            code: Code.PARAM_ERROR
        });
    }

    var player_manager = new PlayerManager(role_id);
    var reward_list = [];
    async.waterfall(
        [
            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(
                    ['rebel', 'role', 'bag', 'build', 'soldier', 'task', 'daily_task', 'achieve', 'sevenDayActivity', 'accrued'],
                    cb);
            },
            function (cb) {
                var rebel_model = player_manager.rebel;
                var role_model = player_manager.role;

                // 判断今天是否已经通关过了
                if (!!rebel_model.isTodayPassRebel(rebel_id)) {
                    return next(null, {
                        code: Code.REBEL_TODAY_HAVE_PASS
                    });
                }

                // 判断这个来袭的之前的难度是否完成
                if (!rebel_model.isPassPreRebel(rebel_id)) {
                    return next(null, {
                        code: Code.REBEL_PRE_NOT_PASS
                    });
                }

                var rebel_id_list = [];
                _.each(vipConf.getRebelByVip(role_model.getVip()).split(','), function (rebel_id) {
                    rebel_id_list.push(parseInt(rebel_id));
                });

                // 是否满足vip等级
                if (_.indexOf(rebel_id_list, rebel_id) == -1) {
                    return next(null, {
                        code: Code.REBEL_VIP_NOT_ENOUGH
                    });
                }

                reward_list = rebelService.getRewardList(rebel_id);
                rewardService.addRewardListToRole(player_manager, reward_list, moduleType.MDL_REBEL, operationType.OPR_REBEL_FIGHT);

                // 增加副本 防守  来袭扫荡  统计
                taskService.addComplete(player_manager);
                rebel_model.passRebel(rebel_id);
                player_manager.save(cb);
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }
            return next(null, {
                code: Code.OK,
                result: {
                    reward_list: reward_list
                }
            });
        }
    );
};
